package com.hyyt.shopjsp.basic.controller;

import com.hyyt.shopjsp.basic.pojo.BasicFunctions;
import com.hyyt.shopjsp.basic.service.IBasicFunctionsService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Zhang Dingfang on 2016/10/24 0024下午 7:07.
 */
@Controller
@RequestMapping("/back/functions")
public class BasicFunctionsController extends BaseController {
    private Logger log = Logger.getLogger(BasicFunctionsController.class);
    private String basePath = "/back/basic/functionPurview/";

    @Resource
    private IBasicFunctionsService basicFunctionsService;


    /**
     * 查询所有信息列表
     * @throws IOException
     */
    @RequestMapping(value = "listFunctions", method = RequestMethod.POST)
    @ResponseBody
    public void listFunctions(String funName,Integer purviewId, Page page, HttpServletResponse response) throws IOException {
        Map<String, Object> param = new HashMap<>();
        if (funName!=null&&!"".equals(funName.trim())) {
            param.put("funName", funName);
        }
        param.put("purviewId", purviewId);
        param.put("sortColumns", "fid asc");
        int totalRecordCount = basicFunctionsService.countBasicFunctions(param);
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        List<Map<String,Object>> functionsList = basicFunctionsService.queryBasicFunctions(param, page.getPageRecordBeginIndex(), page.getPageSize());
        param.clear();
        param.put("total", totalRecordCount);// total键 存放总记录数，必须的
        param.put("rows", functionsList);// rows键 存放每页记录 list
        ResultMsg.create(param, response);  //把内容发送到页面*/
    }

    /**
     * 获取一条记录
     * @throws IOException
     */
    @RequestMapping(value = "getFunctionsObject", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getFunctionsObject(Integer fid) throws IOException{
        BasicFunctions functions = basicFunctionsService.loadBasicFunctions(fid);
        return ResultMsg.create(true, null, FastJsonUtils.toJSONString(functions));
    }

    /**
     * 保存或者修改
     * @param functions
     * @return
     */
    @RequestMapping(value = "saveOrUpdateFunctions", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdateFunctions(BasicFunctions functions){
        if(Utils.objectIsNotEmpty(functions)){
            functions = basicFunctionsService.saveOrUpdateObject(functions);
            if(functions.getFid()!=null){
                return ResultMsg.create(true);
            }
        }
        return ResultMsg.create(false);
    }


    /**
     * 删除
     * @param fid
     * @return
     */
    @RequestMapping(value = "deleteFunctions", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteFunctions(Integer fid){
        boolean success = false;
        if(Utils.objectIsNotEmpty(fid)){
            basicFunctionsService.deleteBasicFunctions(fid);
            success = true;
        }
        return ResultMsg.create(success);
    }
}